from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import re
import pymongo

KEYWORD = '美食'
URL = 'https://www.taobao.com/'
MONGO_URL = 'LOCALHOST'
MONGO_DB = 'Taobao'
MONGO_TABLE = 'food'
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]
mySet = db[MONGO_TABLE]

def save_to_mongo(info):
    if mySet.insert(info):
        print('插入成功',info)
    else:
        print('插入失败',info)

def analize_page(html):
    soup = BeautifulSoup(html,'lxml')
    names = soup.select('.title .J_ClickStat')
    prices = soup.select('.price strong')
    counts = soup.select('.row .deal-cnt')
    shops = soup.select('.shop a')
    addrs = soup.select('.row .location')
    for i in range(len(names)):
        info={
            'name':names[i].get_text().strip(),
            'price':prices[i].get_text().strip(),
            'count': counts[i].get_text().strip(),
            'shop': shops[i].get_text().strip(),
            'addrs': addrs[i].get_text().strip(),
        }
        save_to_mongo(info)


def search(browser,wait):
    try:
        browser.get(URL)
        inputs = browser.find_element_by_id('q')
        inputs.clear()
        inputs.send_keys(KEYWORD)
        inputs.send_keys(Keys.ENTER)

        wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > '
                                                                'div.form > span.btn.J_Submit')))
        rs = browser.find_element_by_css_selector('#mainsrp-pager > div > div > div > div.total').text
        page = re.search(r'(\d+)',rs).group()
        analize_page(browser.page_source)
        return int(page)
    except TimeoutException:
        search(browser,wait)
        print('timeout')
    except NoSuchElementException:
        print('No Element')
    finally:
        browser.close()

def get_next_page(browser,wait,pn):
    try:
        inputs = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > div.form'
                                                                            ' > input')))
        button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#mainsrp-pager > div > div > div '
                                                                        '> div.form > span.btn.J_Submit')))
        inputs.clear()
        inputs.send_keys(pn)
        button.click()

        wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,'#mainsrp-pager > div > div '
                                                                     '> div > ul > li.item.active > span') ,str(pn)))
        analize_page(browser.page_source)

    except TimeoutException:
        get_next_page(browser,wait,pn)
        print('timeout')
def main():
    browser = webdriver.Chrome()
    wait = WebDriverWait(browser, 10)

    page = search(browser,wait)
    for pn in range(2,page+1):
        get_next_page(browser,wait,pn)

    browser.close()

if __name__ == '__main__':
    main()
